var ctx=window.daijia.ctx;
var zj_lng=window.daijia.lng;
var zj_lat=window.daijia.lat;
var zj_map2=null;
var zj_driving=null;
var zj_start = new Array();
var zj_end = new Array();
var zj_way=new Array();
var zj_distance=[];
var zj_timeStr=[];
var zj_flag=true;
var zj_last=false;
var zj_points=[];
var zj_pointObj=[];
var zj_myGeo=null;
var zj_index = 0;
var zj_polylines=[];
var zj_tempmarkers=[];
var zj_count=0;
var zj_pointIndex=1;
var zj_input=null;
var zj_lock=false;
var createflag = false;
var freight_isBlack=false;
function initialize2(){
	zj_myGeo = new BMap.Geocoder();
	var point=new BMap.Point(zj_lng,zj_lat);
	if (point) {
		zj_map2 = new BMap.Map("freight_allmap");
		zj_map2.centerAndZoom(point, 13);
		zj_map2.enableScrollWheelZoom();
	    ///三种驾车策略：最少时间，最短距离，避开高速    BMAP_DRIVING_POLICY_LEAST_TIME,BMAP_DRIVING_POLICY_LEAST_DISTANCE,BMAP_DRIVING_POLICY_AVOID_HIGHWAYS
		zj_driving = new BMap.DrivingRoute(zj_map2, {policy : BMAP_DRIVING_POLICY_LEAST_DISTANCE});
		zj_driving.setSearchCompleteCallback(function(){
	        var pts = zj_driving.getResults().getPlan(0).getRoute(0).getPath(); //通过驾车实例，获得一系列点的数组
	        var plan =zj_driving.getResults().getPlan(0);
	        zj_distance[zj_distance.length]=plan.getDistance(true);
	        zj_timeStr[zj_timeStr.length]=plan.getDuration(true);
	        
	        var polyline = new BMap.Polyline(pts);  
	        zj_polylines[zj_polylines.length]=polyline;
	        zj_map2.addOverlay(polyline);
	        if(zj_flag){
	        	zj_bdGEO();
	        	zj_flag=false;
	        }
	       setTimeout(function(){
	    	   zj_map2.setViewport(zj_points); 
	            //调整到最佳视野
	            $("#freight_truckTypeId").change();
	        },200);
	    });
	   
	    //右键菜单
	    var contextMenu = new BMap.ContextMenu();
		var txtMenuItem = [{
		   text:'设为始发地',
		   callback:function(p){
			   zj_index=0;
			   zj_flag=true;
			   zj_pointObj=[];
			   if(zj_points[0]!=undefined){
				   zj_points.splice(0,1,p);
	    	   }else{
	    		   zj_points[0]=p;
	    	   }
			   if(zj_points.length==1){
				   getFreightEmployee();
				    var marker = new BMap.Marker(p); 
			        zj_myGeo.getLocation(p, function(rs){
						   var addComp = rs.addressComponents;
						   var address=addComp.district + " "+ addComp.street;
						   var lab = new BMap.Label(address,{offset:new BMap.Size(-30,-15)});
							lab.setStyle({ color : "#ED2D2D", fontSize : "16px",border:"0" });
							marker.setLabel(lab); 
							$("#freight_startAddress").val(address);
							$("#freight_startAddress").attr("zj_waypointlng",p.lng);
							$("#freight_startAddress").attr("zj_waypointlat",p.lat);
					    }); 
			       
					    zj_map2.addOverlay(marker);
			   
			   }else if(zj_points.length>1){
				   zj_index=0;
				   zj_flag=true;
				   zj_distance=[];
				   zj_timeStr=[];
				   zj_myGeo.getLocation(p, function(rs){
					   var addComp = rs.addressComponents;
					   var address=addComp.district + " "+ addComp.street;
						$("#freight_startAddress").val(address);
						$("#freight_startAddress").attr("zj_waypointlng",p.lng);
						$("#freight_startAddress").attr("zj_waypointlat",p.lat);
				    }); 
				   zj_map2.clearOverlays();           
					  for(var i=0;i<zj_points.length-1;i++){
						  zj_driving.search(zj_points[i],zj_points[i+1]);
					  }	
				   
			   }
			   
		   }
		},{
		   text:'设为途径地',
		   callback:function(p){
			   if(zj_points.length==12){
				   $.scojs_message("最多只能添加十个途径地", $.scojs_message.TYPE_ERROR);
				   return;
			   }
			   
			   if(zj_points.length>0){
				   zj_index=0;
				   zj_flag=true;
				   zj_distance=[];
				   zj_timeStr=[];
				   zj_points[zj_points.length]=p;
				   zj_map2.clearOverlays();           
					  for(var i=0;i<zj_points.length-1;i++){
						  zj_driving.search(zj_points[i],zj_points[i+1]);
					  }	
				   var end = $("#zj_destination");
				   var marker = new BMap.Marker(p); 
				   if(end.val()==""){
					   zj_myGeo.getLocation(p, function(rs){
						   var addComp = rs.addressComponents;
						   var address=addComp.district + " "+ addComp.street;
						   var lab = new BMap.Label(address,{offset:new BMap.Size(-30,-15)});
							lab.setStyle({ color : "#ED2D2D", fontSize : "16px",border:"0" });
							marker.setLabel(lab); 
							end.val(address);
							end.attr("zj_waypointlng",p.lng);
							end.attr("zj_waypointlat",p.lat);
					    }); 
			       
					    zj_map2.addOverlay(marker);  
					   
					   
				   }else{
						zj_count++;
						  zj_myGeo.getLocation(p, function(rs){
							   var addComp = rs.addressComponents;
							   var address=addComp.district + " "+ addComp.street;
						var str = "<div class='form-group zj_wayAddress' id='zj_waypoint"+(zj_count)+"'>"+
					        "<div class='col-sm-9'>"+
				        	"<div class='input-group'>"+
				        		"<span class='input-group-addon'>途径地址</span>"+
				            	 "<input class='form-control zj_waypointAddress'  zj_index="+(++zj_pointIndex)+"  zj_waypointLng='' onfocus='showData(this)' zj_waypointLat='' id='address"+zj_count+"'/>"+
				            "</div>"+
				            "<div class='show'><font style='font-size:10px'> 联系人:<span class='showContacts'></span></font> <font style='font-size:10px'>联系电话:<span class='showPhone'></span></font></div>"+
				            "<div class='col-sm-12' style='display:none;'>"+
				                 "<div class='input-group col-sm-12'>"+
				                 	   "<span class='input-group-addon'>联系人员</span>"+
						               "<input class='form-control zj_waypointContacts' />"+
				                 "</div>"+
				            "</div>"+
				            "<div class='col-sm-12' style='display:none;'>"+
				                   "<div class='input-group col-sm-12'>"+
				                 	    "<span class='input-group-addon'>联系电话</span>"+
						            	"<input class='form-control zj_waypointPhone'  required/>"+
				                   "</div>"+
				            "</div>"+
						  "</div>"+
						      "<div class='input-group'>"+
			               	 	"<label><span class='glyphicon glyphicon-minus zj_waypointIcon zj_subWaypoint' style='cursor:pointer;' title='删除途径地' zj_index='waypoint"+(zj_count)+"'></span></label>"+
			             	   	"</div>"+
			             "</div>";
						
						 $("#zj_waypointItem").append(str); 
						 
						 $(".zj_subWaypoint").unbind().click(function(){
							 zj_count--;
							 zj_pointIndex--;
							 	$(this).parent().parent().parent().remove();
							 	var index1 = end.attr("zj_index");
								end.attr("zj_index",index1-1);
								var arr = $(".zj_subWaypoint");
								for(var i=0;i<arr.length;i++){
									$(arr[i]).attr("index",("waypoint"+(i+1)));
								}
								var arr = $(".zj_wayAddress");
								for(var i=0;i<arr.length;i++){
									$(arr[i]).attr("id",("waypoint"+(i+1)));
								}
								var address = $(".zj_waypointAddress");
								for(var i=0;i<address.length;i++){
									$(address[i]).attr("zj_index",i);
								}
								var temp = $(this).parent().parent().parent().find(".zj_waypointAddress").attr("zj_index");
								 zj_points.splice(temp,1);
								 zj_map2.clearOverlays();
								 zj_flag=true;
								  zj_index=0;
								  zj_distance=[];
								  zj_timeStr=[];
								 for(var i=0;i<zj_points.length-1;i++){
									  zj_driving.search(zj_points[i],zj_points[i+1]);
								  }	
								 
								
								 
						  });
						 var address1=end.val();
							end.val(address);
							var lng=end.attr("zj_waypointLng");
							end.attr("zj_waypointLng",p.lng);
							var lat=end.attr("zj_waypointLat");
							end.attr("zj_waypointLat",p.lat);
							var phone=$("#zj_end").find("#zj_phone").val();
							$("#zj_end").find("#zj_phone").val("");
							var contacts=$("#zj_end").find("#zj_contacts").val()
							$("#zj_end").find("#zj_contacts").val("");
							var way = $("#zj_waypoint"+zj_count);
							way.find(".zj_waypointAddress").val(address1);
							way.find(".zj_waypointAddress").attr("zj_waypointLng",lng);
							way.find(".zj_waypointAddress").attr("zj_waypointLat",lat);
							way.find(".zj_waypointContacts").val(contacts);
							way.find(".zj_waypointPhone").val(phone);
							var index2 = way.find(".zj_waypointAddress").attr("zj_index");
							var index1 = end.attr("zj_index");
							way.find(".zj_waypointAddress").attr("zj_index",index1);
							end.attr("zj_index",index2);
						    });
						 
			         }
				   
			     }  
			   }
			   
		}];
		
		for(var i=0; i < txtMenuItem.length; i++){
			contextMenu.addItem(new BMap.MenuItem(txtMenuItem[i].text,txtMenuItem[i].callback,100));
			if(i==1 || i==3) {
				contextMenu.addSeparator();
			}
		}
		zj_map2.addContextMenu(contextMenu); 
		

	    //地图加载完毕
	   
	}
	else{
		$.scojs_message("地区"+center+"位置不准确，请重新编辑地区", $.scojs_message.TYPE_ERROR);
	}
	
	
}

function zj_bdGEO(){	
	var pt = zj_points[zj_index];
	zj_geocodeSearch(pt);
	
	zj_index++;
}
function zj_geocodeSearch(pt){
	if(zj_index < zj_points.length-1){
		setTimeout(window.zj_bdGEO,100);
	} 
	zj_myGeo.getLocation(pt, function(rs){
		var addComp = rs.addressComponents;
		var m = new BMap.Marker(pt);
		zj_tempmarkers[zj_tempmarkers.length]=m;
		var  str= addComp.district + " " + addComp.street;
		var lab = new BMap.Label(str,{offset:new BMap.Size(-30,-15)});
		lab.setStyle({ color : "#ED2D2D", fontSize : "16px",border:"0" });
		m.setLabel(lab);
		zj_map2.addOverlay(m);
	});
	 
}


function zj_position(address){
	 if(($(address).parent().parent().parent().parent().parent().parent().parent()).prev().attr("id")!=null&&($(this).parent().parent().parent().parent().parent().parent().parent()).prev().find($(".zj_waypointAddress")).val()==""){
		 $.scojs_message("请从上一个途径地开始填写", $.scojs_message.TYPE_ERROR);
		 return false;
	 };
	var tempIndex=parseInt($(address).attr("zj_index"));
	if($(address).val()==''){
		$.scojs_message("请填写地点", $.scojs_message.TYPE_ERROR);
	}else{
		zj_map2.clearOverlays();
		var local = new BMap.LocalSearch(zj_map2, {
			renderOptions:{map: zj_map2, autoViewport:true}
		});
		
		var length = zj_points.length;
		local.search($(address).val());
		local.setInfoHtmlSetCallback(function(poi,marker,html){
			 
			 var str = ($(html).text().substr(4).split("电话"))[0];
	    	   var string;
	    	   if(str.indexOf("段")==-1){
		    		  if(str.indexOf("路")==-1){
		    			  if(str.indexOf("街")==-1){
		    				  if(str.indexOf("号")==-1){
		    					  string =str;
		    				  }else{
		    					  string=str.substring(0,str.indexOf(/\d+号/.exec(str)[0]));
		    				  }
		    			  }else{
		    				 string = str.substring(0,str.indexOf("街")+1);
		    			  }
		    		  }else{
		    			  string = str.substring(0,str.indexOf("路")+1);
		    		  }
		    	   }else{
		    		   string = str.substring(0,str.indexOf("段")+1);
		    	   }	
	    	   $(address).val(string+poi.marker.getTitle());
	    	   $(address).attr("zj_waypointLng",poi.marker.point.lng);
	    	   $(address).attr("zj_waypointLat",poi.marker.point.lat);
	    	   zj_index=0;
	    	   zj_flag=true;
	    	   if(zj_points[tempIndex]!=undefined){
	    		   zj_points.splice(tempIndex,1,poi.point);
	    	   }else{
	    		   zj_points[length]=poi.point;
	    	   }
			 //  points[length]=poi.point;
			   
	    	   if(zj_points.length==1){
					 var markers = zj_map2.getOverlays();
					for(var i=0;i<markers.length;i++){
						markers[i].show();
					}
			   }else if(zj_points.length>1){
				   var markers = zj_map2.getOverlays();
					for(var i=0;i<markers.length;i++){
						markers[i].show();
					}
					for(var i=0;i<zj_tempmarkers.length;i++){
						zj_tempmarkers[i].hide();
					}
					poi.marker.hide(); 
					for(var j=0;j<zj_polylines.length;j++){
						zj_map2.removeOverlay(zj_polylines[j]);
					}
					
					
					 zj_distance=[];
					 zj_timeStr=[];
				  for(var i=0;i<zj_points.length-1;i++){
					  zj_driving.search(zj_points[i],zj_points[i+1]);
				  }	 
				 
			   }
	    	   
	    });
	}

}
//推荐附近员工
function getFreightEmployee(){
	    if($("#freight_truckTypeId").val()==""){
	    	$("#freight_tbodyId").empty();
	    }else{
	    	$.get(ctx+'/freight/getEmployees',{
		    	startLat:zj_points[0].lat,
		    	startLng:zj_points[0].lng,
		    	truckTypeId:$("#freight_truckTypeId").val()
		    	},function(data){
		    	$(".freight_drivers-tr").remove();
				if(data && data.length>0){
					$(data).each(function(i,driver){
						var content="<tr class='freight_drivers-tr'>"+
									"<td><input type='radio' name='freihgt_checkDriver' value='"+driver.employId+"' /></td>"+
									"<td>"+driver.employName+"</td>"+
									"<td>"+driver.carNumber+"</td>"+
									"<td>"+driver.distance+"</td>"+
									"<tr>";
						$("#freight_tbodyId").append(content);
					});
				}
			});
	    }
}

function showData(add){
	zj_input = $(add).parent().parent();
	$('#myModal').modal({backdrop:true});
	$("#tempAddress").val($(add).parent().parent().find(".zj_waypointAddress").val());
    $("#tempContacts").val($(add).parent().parent().find(".zj_waypointContacts").val());
    $("#tempPhone").val(($(add).parent().parent().find(".zj_waypointPhone")).val());
}

$(document).ready(function(){

	
	initialize2();

	$('#zj_payMount').bind('input propertychange', function() {
		var val = $(this).val();
		var p = val.split(".");
		if(window.parseFloat($(this).val())>window.parseFloat($(this).attr("max"))){
	      $.scojs_message('输入数值过大', $.scojs_message.TYPE_ERROR);
	      $(this).val("");
	    }else if(window.parseFloat($(this).val())<1){
	        $.scojs_message('输入数值过小', $.scojs_message.TYPE_ERROR);
	        $(this).val("");
	    }
		if(p.length>1){
	    	if(p[1].length>2){
	    		 $.scojs_message('小数位数限定为两位小数', $.scojs_message.TYPE_ERROR);
	    		 $(this).val(p[0]+"."+p[1].substr(0,2));
	    	}
	    }
	    
	});
//时间格式化
	Date.prototype.Format = function(fmt) 
	{ 
	  var o = { 
	    "M+" : this.getMonth()+1,                 //月份 
	    "d+" : this.getDate(),                    //日 
	    "h+" : this.getHours(),                   //小时 
	    "m+" : this.getMinutes(),                 //分 
	    "s+" : this.getSeconds(),                 //秒 
	    "q+" : Math.floor((this.getMonth()+3)/3), //季度 
	    "S"  : this.getMilliseconds()             //毫秒 
	  }; 
	  if(/(y+)/.test(fmt)) 
	    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
	  for(var k in o) 
	    if(new RegExp("("+ k +")").test(fmt)) 
	  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
	  return fmt; 
	}
	
	
	$(window).scroll(function(){  
		$("#freight_allmap").css({ position:'fixed',left:'22px',top:'',width:'555px'});
	});
	$.get(ctx+'/member/findByPhone',{'phone':$("#freight_passengerPhone").val(),'couponType':'freight'},function(data){
		if(data.passenger.inBlackList){
			$("#freight_membertype").html("黑名单");
			freight_isBlack=true;
			isBlack=true;
		}else{
			$("#freight_membertype").html(data.passenger.typeStr+(data.passenger.grade_name==null?"":("-"+data.passenger.grade_name)));
		}
		
	    $("#freight_clientVirtual").text(data.passenger.drawMoney+"元");
		if(data.coupons!=null){
			    var couponId = $("#temp_freight_couponId").val();
			  	if(data.coupons.length>0){
					var text="";
					for(var i=0;i<data.coupons.length;i++){
						text+= "<option value='"+data.coupons[i].id+"'  "+(data.coupons[i].id==couponId?("selected"):(""))+">"+data.coupons[i].couponRule.title+"</option>";
					}
					$("#freight_couponId").empty();
					if(text!=""){
						$("#freight_couponInput").css("display","block");
						$("#freight_couponId").append(text);
					}
					
				}
			  
				
		}});
			
	$("#zj_dialog_order").scroll(function(){  
		$("#zj_allmap2").css({ position:'fixed',left:'451px',top:'174px',width:'457px'});
	});
	
	//清除资源
	$("#zj_close").click(function(){
		 zj_points=[];
		 zj_pointObj=[];
		 zj_index = 0;
		 zj_polylines=[];
	     zj_tempmarkers=[];
		 zj_count=0;
	     zj_pointIndex=1;
	     $("#zj_waypointItem").empty();
	     $("#zj_destination").val("");
	     $("#zj_destination").attr("zj_index",1);
	     $("#zj_destination").attr("zj_waypointLng","");
	     $("#zj_destination").attr("zj_waypointLat","");
	     $("#zj_contacts").val("");
	     $("#zj_phone").val("");
	});
	$("#tempContacts").change(function(){
		zj_input.find(".zj_waypointContacts").val($("#tempContacts").val());
		zj_input.find(".showContacts").text($("#tempContacts").val());
	});
	$("#tempPhone").change(function(){
		zj_input.find(".zj_waypointPhone").val($("#tempPhone").val());
		zj_input.find(".showPhone").text($("#tempPhone").val());
		
	});

	$("#freightfixPrice").click(function(e){
		if(this.checked){
			$("#freightfixPrice_div").slideDown();
		}
		else{
			$("#freightfixPrice_div").slideUp();
		}
	});
	
	var content=null;
	$("#zj_addAddress").click(function(){
		if($("#zj_destination").val()==""){
			$.scojs_message("请依次填写地址", $.scojs_message.TYPE_ERROR);
			return;
		}
		 zj_last=false;
		if($(".zj_wayAddress").length>=12){
			$.scojs_message("最多只能添加十个途径地", $.scojs_message.TYPE_ERROR);
			return;
		}
		if(!($(".zj_wayAddress").length>0)){
			zj_count=0;
		}
		zj_count++;
		
		 content = "<div class='form-group zj_wayAddress' id='zj_waypoint"+(zj_count)+"'>"+
	        "<div class='col-sm-9'>"+
        	"<div class='input-group'>"+
        		"<span class='input-group-addon'>途径地址</span>"+
            	 "<input class='form-control zj_waypointAddress'  zj_index="+(++zj_pointIndex)+"  zj_waypointLng='' onfocus='showData(this)' zj_waypointLat='' id='address"+zj_count+"'/>"+
            "</div>"+
            "<div class='show'><font style='font-size:10px'> 联系人:<span class='showContacts'></span></font> <font style='font-size:10px'>联系电话:<span class='showPhone'></span></font></div>"+
            "<div class='col-sm-12' style='display:none;'>"+
                 "<div class='input-group col-sm-12'>"+
                 	   "<span class='input-group-addon'>联系人员</span>"+
		               "<input class='form-control zj_waypointContacts' />"+
                 "</div>"+
            "</div>"+
            "<div class='col-sm-12' style='display:none;'>"+
                   "<div class='input-group col-sm-12'>"+
                 	    "<span class='input-group-addon'>联系电话</span>"+
		            	"<input class='form-control zj_waypointPhone'  required/>"+
                   "</div>"+
            "</div>"+
			  "</div>"+
			"<div class='input-group'>"+
			//	"<label><img src='"+ctx+"/assets/css/images/minus_8.png' class=' waypointIcon subWaypoint' style='cursor:pointer;' title='删除途径地' index='waypoint"+count+"'/></label>"+
			"<label><span class='glyphicon glyphicon-minus zj_waypointIcon zj_subWaypoint' style='cursor:pointer;' title='删除途径地' index='waypoint"+zj_count+"'></span></label>"+
				"</div>"+
			"</div>";
		 $("#zj_waypointItem").append(content); 
		 $(".zj_addWaypoint").unbind().click(function(){
			 $(".zj_addAddress").click();
			 zj_last=false;
		 });
		 var end = $("#zj_end").find("#zj_destination");
		 $(".zj_subWaypoint").unbind().click(function(){
			 zj_count--;
			 zj_pointIndex--;
			 	$(this).parent().parent().parent().remove();
			 	var index1 = end.attr("zj_index");
				end.attr("zj_index",index1-1);
				var arr = $(".zj_subWaypoint");
				for(var i=0;i<arr.length;i++){
					$(arr[i]).attr("index",("waypoint"+(i+1)));
				}
				var arr = $(".zj_wayAddress");
				for(var i=0;i<arr.length;i++){
					if(i==0){
						$(arr[i]).attr("id","zj_start");
					}else if(i==(arr.length-1)){
						$(arr[i]).attr("id","zj_end");
					}else{
						$(arr[i]).attr("id","zj_waypoint"+(i+1));
					}
				}
				var address = $(".zj_waypointAddress");
				for(var i=0;i<address.length;i++){
					$(address[i]).attr("zj_index",i);
				}
				
				var temp = $(this).parent().parent().parent().find(".zj_waypointAddress").attr("zj_index");
				 zj_points.splice(temp,1);
				 zj_map2.clearOverlays();
				 zj_flag=true;
				  zj_index=0;
				  zj_distance=[];
				  zj_timeStr=[];
				 for(var i=0;i<zj_points.length-1;i++){
					  zj_driving.search(zj_points[i],zj_points[i+1]);
				  }	
		  });
		 
			var zj_lastoperate="";
		 
		 $(".zj_waypointIcon").mouseover(function(){
			 $(this).addClass("icon-2x");		
		 });
		 $(".zj_waypointIcon").mouseout(function(){
			 $(this).removeClass("icon-2x");		
		 });	
		 var end = $("#zj_end").find("#zj_destination");
			var address=end.val();
			end.val("");
			var lng=end.attr("zj_waypointlng");
			end.attr("zj_waypointlng","");
			var lat=end.attr("zj_waypointlat");
			end.attr("zj_waypointlat","");
			var phone=$("#zj_end").find("#zj_phone").val();
			$("#zj_end").find("#zj_phone").val("");
			$("#end").find(".showPhone").empty();
			var contacts=$("#zj_end").find("#zj_contacts").val()
			$("#zj_end").find("#zj_contacts").val("");
			$("#end").find(".showContacts").empty();
			var way = $("#zj_waypoint"+zj_count);
			way.find(".zj_waypointAddress").val(address);
			way.find(".zj_waypointAddress").attr("zj_waypointLng",lng);
			way.find(".zj_waypointAddress").attr("zj_waypointLat",lat);
			way.find(".zj_waypointContacts").val(contacts);
			way.find(".zj_waypointPhone").val(phone);
			way.find(".showContacts").text(contacts);
			way.find(".showPhone").text(phone);
			var index2 = way.find(".zj_waypointAddress").attr("zj_index");
			var index1 = $("#zj_destination").attr("zj_index");
			way.find(".zj_waypointAddress").attr("zj_index",index1);
			end.attr("zj_index",index2);
			if(zj_lock){
				showData($("#zj_destination"));
			}
	});
	if(waypos.length>0){
		count=0;
		for(var i=0;i<waypos.length;i++){
			if(waypos[i].sequence==0){
				var start = $("#zj_start").find(".zj_waypointAddress");
				start.val(waypos[i].address);
				    start.attr("zj_index",0);
				    start.attr("zj_waypointLng",waypos[i].lng);
				    start.attr("zj_waypointLat",waypos[i].lat);
				$("#zj_start").find(".zj_waypointContacts").val(waypos[i].contacts);
				$("#zj_start").find(".zj_waypointPhone").val(waypos[i].phone);
				$("#zj_start").find(".showContacts").text(waypos[i].contacts);
				$("#zj_start").find(".showPhone").text(waypos[i].phone);
				zj_points[0] =  new BMap.Point(waypos[i].lng,waypos[i].lat);
			}else{
				 if($("#zj_end").find(".zj_waypointAddress").val()!=""){
					 $("#zj_addAddress").click();
					 var end = $("#zj_end").find(".zj_waypointAddress");
					    end.val(waypos[i].address);
					    end.attr("zj_waypointLng",waypos[i].lng);
					    end.attr("zj_waypointLat",waypos[i].lat);
					    $("#zj_end").find(".zj_waypointContacts").val(waypos[i].contacts);
						$("#zj_end").find(".zj_waypointPhone").val(waypos[i].phone);
						$("#zj_end").find(".showContacts").text(waypos[i].contacts);
						$("#zj_end").find(".showPhone").text(waypos[i].phone);
						zj_points[zj_points.length] =  new BMap.Point(waypos[i].lng,waypos[i].lat);
				}else{
					var end = $("#zj_end").find(".zj_waypointAddress");
				    end.val(waypos[i].address);
				    end.attr("zj_waypointLng",waypos[i].lng);
				    end.attr("zj_waypointLat",waypos[i].lat);
				    $("#zj_end").find(".zj_waypointContacts").val(waypos[i].contacts);
					$("#zj_end").find(".zj_waypointPhone").val(waypos[i].phone);
					$("#zj_end").find(".showContacts").text(waypos[i].contacts);
					$("#zj_end").find(".showPhone").text(waypos[i].phone);
					zj_points[zj_points.length] =  new BMap.Point(waypos[i].lng,waypos[i].lat);
				}
				if(i==waypos.length-1){
						zj_lock=true;
			    }
					
			}
		
		}
		   zj_index=0;
    	   zj_flag=true;
		 zj_distance=[];
		 zj_timeStr=[];
	  for(var i=0;i<zj_points.length-1;i++){
		  zj_driving.search(zj_points[i],zj_points[i+1]);
	  }	 
		
	}
	$("#freight_companyId").change(function(){
		var select = $("#freight_truckTypeId");
		if($(this).val()!=""){
		$.get(ctx+'/freight/findByTruckTypeCompanyId',{'companyId':$(this).val()},function(data){
		    select.children('option').remove();
		    var truckTypes = data.truckTypes;
		    if(truckTypes.length==0){
		    	select.children('option').remove()
				var content = "<option value=''>此公司没有货车类型</option>";
		    	select.append($(content));
		    	$("#zj_budgetPay").val("");
		    	$("#zj_startPrice").val("");
		    	$("#zj_mileagePrice").val("");
		    	$("#zj_runTimePrice").val("");
		    	$("#zj_runTime").val("");
		    	$("#zj_mileage").val("");
		    	$(".zj_runTime").text("");
		    	$(".zj_mileage").text("");
		    	getFreightEmployee();
		    }
		    for(var i=0;i<truckTypes.length;i++){
		    	
		    	var str = '';
		    	if(truckTypes[i].cangointotown){
		    		str='--入城';
		    	}
		    	var content = "<option value='"+truckTypes[i].id+"'>"+truckTypes[i].typeName+str+"</option>";
		    	select.append($(content));
		    	$("#freight_truckTypeId").change();
		    }
			
		},'json');
		}else{
			select.children('option').remove()
			var content = "<option value=''>请先选择公司</option>";
	    	select.append($(content));
	    	
		}
		
		
	});
	$("#freight_passengerName").change(function(){
		$("#zj_start").find(".zh_waypointContacts").val($(this).val());
	}); 
	 
	$("#freight_passengerPhone").change(function(){
		$.get(ctx+'/member/findByPhone',{'phone':$(this).val(),'couponType':'freight'},function(data){
			if(data.passenger!=null){
				$("#freight_companyName").val(data.passenger.companyName);
				$("#freight_passengerId").val(data.passenger.id);
				$("#freight_passengerName").val(data.passenger.name);
				$("#freight_membertype").text("");
				$("#zj_start").find(".zj_waypointContacts").val($("#freight_passengerName").val());
				$("#zj_start").find(".zj_waypointPhone").val($("#freight_passengerPhone").val());
				$("#zj_start").find(".showContacts").html($("#freight_passengerName").val());
				$("#zj_start").find(".showPhone").html($("#freight_passengerPhone").val());
				if(data.passenger.inBlackList){
					$("#freight_membertype").html("黑名单");
					freight_isBlack=true;
				}else{
					$("#freight_membertype").html(data.passenger.typeStr+(data.passenger.grade_name==null || data.passenger.grade_name==""?"":("-"+data.passenger.grade_name)));
				}
			    $("#freight_clientVirtual").text(data.passenger.drawMoney+"元");
				if(data.coupons!=null&&data.coupons.length>0){
					var text="";
					for(var i=0;i<data.coupons.length;i++){
						text+= "<option value='"+data.coupons[i].id+"'>"+data.coupons[i].couponRule.title+"</option>";
					}
					$("#freight_couponId").empty();
					if(text!=""){
						$("#freight_couponInput").css("display","block");
						$("#freight_couponId").append(text);
					}
				}else{
					$("#freihgt_couponInput").css("display","none");
					$("#freight_couponId").empty();
				}
				$("#freight_companyId option").each(function(index,element){
				  	if($(element).attr("value")==data.passenger.companyId){
				 		$(element).attr("selected","selected");
				 		$("#freight_companyId").change();
				 	}
				});
				
			}else{
				$("#freight_couponInput").css("display","none");
				$("#freight_couponId").empty();
				$("#freight_passengerName").val("先生/女士");
				$("#freight_membertype").text("新客户");
				$("#zj_start").find(".zj_waypointContacts").val($("#freight_passengerName").val());
				$("#zj_start").find(".zj_waypointPhone").val($("#freight_passengerPhone").val());
				$("#zj_start").find(".showContacts").html($("#freight_passengerName").val());
				$("#zj_start").find(".showPhone").html($("#freight_passengerPhone").val());
			}
			
		});
		
		if(zj_timeStr!=null&&zj_timeStr.length>0&&zj_distance!=null&&zj_distance.length>0){
			 $.post(ctx+"/freight/getPrice",
					   {
				        companyId:isMain?$("#freight_companyId").val():"",
				 		truckTypeId:$("#freight_truckTypeId").val(),
					    time:zj_timeStr.join(","),
					    mileage:zj_distance.join(",")},function(data){
					    	if(data.success){
					    		$("#freight_budgetPay").val(data.money);
						    	$("#freight_startPrice").val(data.startPrice);
						    	$("#freight_mileagePrice").val(data.mileagePrice);
						    	$("#freight_runTimePrice").val(data.travelTimePrice);
						    	$("#freight_runTime").val(data.travelTime);
						    	$("#freight_mileage").val(data.mileage);
						    	$(".freight_runTime").text("预计时间:"+data.travelTime+"分钟");
						    	$(".freight_mileage").text("预计路程:"+data.mileage+"公里");
					    	}else{
					    		$("#freight_budgetPay").val(data.money);
					    		$(".freight_runTime").text("预计价格失败");
					    	}
					    },"json");
		}
		
	});	
	$("#freight_truckTypeId").change(function(){
		if(zj_points[0]!=null){
			getFreightEmployee();
		}
		if(zj_timeStr!=null&&zj_timeStr.length>0&&zj_distance!=null&&zj_distance.length>0){
			 $.post(ctx+"/freight/getPrice",
					   {
				 		companyId:isMain?$("#freight_companyId").val():"",
				 		truckTypeId:$("#freight_truckTypeId").val(),
					    time:zj_timeStr.join(","),
					    mileage:zj_distance.join(",")},function(data){
					    	if(data.success){
					    		$("#freight_budgetPay").val(data.money);
						    	$("#freight_startPrice").val(data.startPrice);
						    	$("#freight_mileagePrice").val(data.mileagePrice);
						    	$("#freight_runTimePrice").val(data.travelTimePrice);
						    	$("#freight_runTime").val(data.travelTime);
						    	$("#freight_mileage").val(data.mileage);
						    	$(".freight_runTime").text("预计时间:"+data.travelTime+"分钟");
						    	$(".freight_mileage").text("预计路程:"+data.mileage+"公里");
					    	}else{
					    		$("#freight_budgetPay").val(data.money);
					    		$(".freight_runTime").text("预计价格失败");
					    	}
					    },"json");
		}
	});
	
	$(".zj_close_modal").click(function(){
		$("#zj_dialog_order").modal("hide");
		$("#zj_waypointItem").empty();
		zj_map2.clearOverlays();
		zj_start=new Array();
		zj_end=new Array();
		zj_way=new Array();
		 zj_points=[];
		 zj_pointObj=[];
		 zj_index = 0;
		 zj_polylines=[];
	     zj_tempmarkers=[];
		 zj_count=0;
	     zj_pointIndex=1;
		
	});
	$("#zj_close_modal").click(function(){
		$("#zj_dialog_order").modal("hide");
		$("#zj_waypointItem").empty();
		zj_map2.clearOverlays();
		zj_start=new Array();
		zj_end=new Array();
		zj_way=new Array();
		 zj_points=[];
		 zj_pointObj=[];
		 zj_index = 0;
		 zj_polylines=[];
	     zj_tempmarkers=[];
		 zj_count=0;
	     zj_pointIndex=1;
		
	});
	
	var f=false;
	var g=false;
	var z=false;
	$("input[name=zj_receivedPay]").click(function(){
		if(!zj_receivedPay){
			
			$(".zj_receivedPay").css("display","inline-block ");
			zj_receivedPay=true;
		}else{
			zj_receivedPay=false;
			$(".zj_receivedPay").css("display","none");
		}
		
	});
	$("input[name=zj_receipt]").click(function(){
		if(!zj_receipt){		
			zj_receipt=true;
		}else{
			zj_receipt=false;
		}
		
	});
	$("input[name=zj_flitting]").click(function(){
		if(!zj_flitting){		
			zj_flitting=true;
		}else{
			zj_flitting=false;
			
		}
		
	});
	//追加
	
	$(".zhuijia").unbind().click(function(){
		var orderId = $(this).attr("orderId");
		$.get(ctx+'/freight/findByOrderId',{"orderId":orderId},function(data){
             $("#zj_passengerId").val(data.order.passengerId);
			 $("#zj_startPrice").val(data.order.startPrice);
			 $("#zj_mileagePrice").val(data.order.mileagePrice);
			 $("#zj_runTimePrice").val(data.order.runTimePrice);
			 $("#zj_runTime").val(data.order.runTime);
			 $("#zj_mileage").val(data.order.mileage); 
			 $("#zj_employId").val(data.order.employId); 
			 $("#zj_passengerPhone").val(data.order.passengerPhone); 
			 $("#zj_passengerName").val(data.order.passengerName); 
			 $("#zj_companyId").val(data.order.companyId); 
			 $("#zj_startTime").val(new Date(data.order.startTime).Format("yyyy-MM-dd hh:mm"));
			 $("#zj_truckTypeId").val(data.order.truckTypeId);
			 $("#zj_budgetPay").val(data.order.budgetPay);
			 if(data.order.flitting&&data.order.flitting!=null){
				 $("input[name=zj_flitting]").attr("checked","checked");
				 zj_flitting=true;
			 }
			 if(data.order.receipt&&data.order.receipt!=null){
				 $("input[name=zj_receipt]").attr("checked","checked");
				 zj_receipt=true;
			 }
			 
			 if(data.order.receivedPay&&data.order.receivedPay!=null){
				 $("input[name=zj_receivedPay]").attr("checked","checked");
				 $(".zj_receivedPay").css("display","inline-block");
				 $("input[name=zj_payMount]").val(data.order.payMount);
				 zj_receivedPay=true;
			 }
			if(data.order.mileage!=null&&data.order.runTime!=null){
			  $(".zj_runTime").text("预计时间:"+data.order.runTime+"分钟");
		      $(".zj_mileage").text("预计路程:"+data.order.mileage+"公里");
			}
			
			var waypos = data.order.waypoints
			if(waypos.length>0){
				count=0;
				for(var i=0;i<waypos.length;i++){
					if(waypos[i].sequence==0){
						var start = $("#zj_start").find(".zj_waypointAddress");
						start.val(waypos[i].address);
						    start.attr("zj_index",0);
						    start.attr("zj_waypointLng",waypos[i].lng);
						    start.attr("zj_waypointLat",waypos[i].lat);
						$("#zj_start").find(".zj_waypointContacts").val(waypos[i].contacts);
						$("#zj_start").find(".zj_waypointPhone").val(waypos[i].phone);
						zj_points[0] =  new BMap.Point(waypos[i].lng,waypos[i].lat);
					}else{
						 if($("#zj_end").find(".zj_waypointAddress").val()!=""){
							$(".zj_addAddress").click();
							 var end = $("#zj_end").find(".zj_waypointAddress");
							    end.val(waypos[i].address);
							    end.attr("zj_waypointLng",waypos[i].lng);
							    end.attr("zj_waypointLat",waypos[i].lat);
							    $("#zj_end").find(".zj_waypointContacts").val(waypos[i].contacts);
								$("#zj_end").find(".zj_waypointPhone").val(waypos[i].phone);
								zj_points[zj_points.length] =  new BMap.Point(waypos[i].lng,waypos[i].lat);
						}else{
							var end = $("#zj_end").find(".zj_waypointAddress");
						    end.val(waypos[i].address);
						    end.attr("zj_waypointLng",waypos[i].lng);
						    end.attr("zj_waypointLat",waypos[i].lat);
						    $("#zj_end").find(".zj_waypointContacts").val(waypos[i].contacts);
							$("#zj_end").find(".zj_waypointPhone").val(waypos[i].phone);
							zj_points[zj_points.length] =  new BMap.Point(waypos[i].lng,waypos[i].lat);
						}
							
					}
				
				}
				   zj_index=0;
		    	   zj_flag=true;
				 zj_distance=[];
				 zj_timeStr=[];
			  for(var i=0;i<zj_points.length-1;i++){
				  zj_driving.search(zj_points[i],zj_points[i+1]);
			  }	 
				
			}
			
			
			
			
			 $("#zj_dialog_order").modal();
		},"json");
		
	});


	//下单
	$("#freight_saveOrder").click(function(){
		if(createflag){
			console.debug("操作繁忙,请稍后再试.");
			$("#freight_saveOrder").attr("disabled","disabled");
			return;
		}
		var temp=$("input[name='freihgt_checkDriver']:checked").val();
		var tempEmployId=null;
		if(temp){
			tempEmployId=temp;
		}

		var ways = $(".zj_wayAddress");
		var temp="";
		for(var i=0;i<ways.length;i++){
			if(i==(ways.length-1)){
				var address = $(ways[i]).find($(".zj_waypointAddress")).val();
				var person = $(ways[i]).find($(".zj_waypointContacts")).val();
				var phone = $(ways[i]).find($(".zj_waypointPhone")).val();
				var waypointLng = $(ways[i]).find($(".zj_waypointAddress")).attr("zj_waypointLng");
				var waypointLat = $(ways[i]).find($(".zj_waypointAddress")).attr("zj_waypointLat");
				 temp=temp+'{"phone":"'+phone+'",';
				 temp=temp+'"contacts":"'+person+'",';
				 temp=temp+'"address":"'+address+'",';
				 temp=temp+'"lng":"'+waypointLng+'",';
				 temp=temp+'"lat":"'+waypointLat+'",';
				 temp=temp+'"sequence":"'+(i)+'"}';
			}else{
				var address = $(ways[i]).find($(".zj_waypointAddress")).val();
				var person = $(ways[i]).find($(".zj_waypointContacts")).val();
				var phone = $(ways[i]).find($(".zj_waypointPhone")).val();
				var waypointLng = $(ways[i]).find($(".zj_waypointAddress")).attr("zj_waypointLng");
				var waypointLat = $(ways[i]).find($(".zj_waypointAddress")).attr("zj_waypointLat");
				 temp=temp+'{"phone":"'+phone+'",';
				 temp=temp+'"contacts":"'+person+'",';
				 temp=temp+'"address":"'+address+'",';
				 temp=temp+'"lng":"'+waypointLng+'",';
				 temp=temp+'"lat":"'+waypointLat+'",';
				 temp=temp+'"sequence":"'+(i)+'"},';
			}
			
		}
		/*if(freight_isBlack){
			$.scojs_message("下单失败", $.scojs_message.TYPE_ERROR);
			return;
		}
		*/
		if($("#freight_companyId").val()==''){
			$.scojs_message("请选择公司", $.scojs_message.TYPE_ERROR);
			return;
		}

		if($("#freight_truckTypeId").val()==""){
			$.scojs_message("请选择货车类型", $.scojs_message.TYPE_ERROR);
			return;
		}
		if(temp==""){
			$.scojs_message("请填写地址", $.scojs_message.TYPE_ERROR);
			return;
		}
		var waypoints = "["+temp+"]";
		
		if($("#freight_orderMemo").val()!=""&&$("#freight_orderMemo").val().length>100){
			$.scojs_message("备注信息过长", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#freight_passengerPhone").val()==''){
			$.scojs_message("请填写客户电话", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#freight_passengerName").val()==''){
			$.scojs_message("请填写客户姓名", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#freight_startAddress").val()==''){
			$.scojs_message("请填写预约地", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#freight_destination").val()==''){
			$.scojs_message("请填写目的地", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#zj_phone").val()==""){
			$.scojs_message("请填写目的地联系电话", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#freight_startTime").val()==""){
			$.scojs_message("请填写预约时间", $.scojs_message.TYPE_ERROR);
			return;
		}else{
			if(new Date($("#freight_startTime").val().replace("-","/")).getTime()-new Date().getTime()<0){
				$("#freight_startTime").val(new Date().Format("yyyy-MM-dd hh:mm"));
			}		
		}
		if(zj_receivedPay){
			if($("[name='zj_payMount']").val()==""){
				$.scojs_message("请填写回款金额", $.scojs_message.TYPE_ERROR);
				return;
			}
		}

		if($("#freightfixPrice").length>0 && $("#freightfixPrice")[0].checked){
			if($("#freightfixPriceMoney").val()==""){
				$.scojs_message("请填写定价金额", $.scojs_message.TYPE_ERROR);
				return false;
			}
			var moneyReg=/^(\d+)(.[0-9]{0,1})?$/;
			if(!moneyReg.test($("#freightfixPriceMoney").val())){
				$.scojs_message("请填写正确的定价金额，如：39.9", $.scojs_message.TYPE_ERROR);
				return false;
			}
		}
		createflag = true;

			$.ajax({
	             type: "POST",
	             url: ctx+"/freight/create",
	             data: {
	            	 passengerId:$("#freight_passengerId").val(),
	            	 passengerName:$("#freight_passengerName").val(),
	            	 passengerPhone:$("#freight_passengerPhone").val(),
	            	 companyId:$("#freight_companyId").val(),
	            	 companyName:$('#freight_companyId').find("option:selected").text(),
	            	 truckTypeId:$("#freight_truckTypeId").val(),
	            	 truckTypeName:($('#freight_truckTypeId').find("option:selected").text().split("--"))[0],
	            	 startTime:$("#freight_startTime").val(),
	            	 budgetPay:$("#freight_budgetPay").val(),
	            	 preStartPrice:$("#freight_startPrice").val(),
	            	 preMileagePrice:$("#freight_mileagePrice").val(),
	            	 preRunTimePrice:$("#freight_runTimePrice").val(),
	            	 preRunTime:$("#freight_runTime").val(),
	            	 preMileage:$("#freight_mileage").val(),
	            	 couponId:$("#freight_couponId").val(),
	            	 flitting:zj_flitting,
	            	 receipt:zj_receipt,
	            	 receivedPay:zj_receivedPay,
	            	 payMount:$("[name='zj_payMount']").val(),
	            	 memo:$("#freight_orderMemo").val(),
	            	 employId:tempEmployId,
					 fixPrice:$("#freightfixPrice").is(':checked'),
					 fixPriceMoney:$("#freightfixPrice").is(':checked')?$("#freightfixPriceMoney").val():"",
	            	 waypoint:waypoints
	             },	
	             dataType: "json",
	             success: function(data){
					 createflag = false;
					 $("#freight_saveOrder").removeAttr("disabled");
	            	 if(data.success){
	            		 $.scojs_message("下单成功", $.scojs_message.TYPE_OK);
	            		 $('#zj_dialog_order').modal("hide");
	            		 issubmit=true;
	            		 window.opener.location.reload(true);
	            		 setTimeout(function(){
		            		 window.close();
		     				},500);
	            		 
	            	 }else{
	            		 $.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
	            	 }
	             },
				error:function(){
					createflag = false;
					$("#freight_saveOrder").removeAttr("disabled");
				}
	      });
	});
	
	$("#freight_close_modal").click(function(){
		 issubmit=true;
		 window.close();
	});
	
	
	
	//定位
	$("#zj_btn-startAddress").click(function(){
		if($("#zj_startAddress").val()==''){
			$.scojs_message("请填写预约地", $.scojs_message.TYPE_ERROR);
		}else{
			map2.clearOverlays();
			var local = new BMap.LocalSearch(map2, {
				renderOptions:{map: map2, autoViewport:true}
			});
			local.search($("#zj_startAddress").val());
			
			local.setInfoHtmlSetCallback(function(poi,html){
		    	   var str = ($(html).text().substr(4).split("电话"))[0];
		    	   var string;
		    	   if(str.indexOf("段")==-1){
			    		  if(str.indexOf("路")==-1){
			    			  if(str.indexOf("街")==-1){
			    				  if(str.indexOf("号")==-1){
			    					  string =str;
			    				  }else{
			    					  string=str.substring(0,str.indexOf(/\d+号/.exec(str)[0]));
			    				  }
			    			  }else{
			    				 string = str.substring(0,str.indexOf("街")+1);
			    			  }
			    		  }else{
			    			  string = str.substring(0,str.indexOf("路")+1);
			    		  }
			    	   }else{
			    		   string = str.substring(0,str.indexOf("段")+1);
			    	   }	
		    	   
		    	   $("#zj_startAddress").val(string+poi.marker.getTitle());
		    	   $("#zj_startLng").val(poi.marker.point.lng);
			       $("#zj_startLat").val(poi.marker.point.lat);
			        start[0]=poi.marker.point.lng;
			        start[1]=poi.marker.point.lat;
			    	var title = poi.marker.getTitle();
			        var markers = zj_map2.getOverlays();
					for(var i=0;i<markers.length;i++){
						markers[i].show();
					}
			        if(end.length!=0){
			        	var startPoint=new BMap.Point(start[0], start[1]);
						var endPoint=new BMap.Point(end[0],end[1]);
						driving.search(startPoint, endPoint);
					    poi.marker.hide(); 
			        }
		    });
		}
	});
	
	$("#zj_btn-destination").click(function(){
		if($("#zj_destination").val()==''){
			$.scojs_message("请填写目的地", $.scojs_message.TYPE_ERROR);
		}else{
			zj_map2.clearOverlays();
			var local = new BMap.LocalSearch(zj_map2, {
	    	  renderOptions:{map: zj_map2, autoViewport:true}
	    	});
		    local.search($("#zj_destination").val());
		    local.setInfoHtmlSetCallback(function(poi,html){
		    	 var str = ($(html).text().substr(4).split("电话"))[0];
		    	   var string;
		    	   if(str.indexOf("段")==-1){
			    		  if(str.indexOf("路")==-1){
			    			  if(str.indexOf("街")==-1){
			    				  if(str.indexOf("号")==-1){
			    					  string =str;
			    				  }else{
			    					  string=str.substring(0,str.indexOf(/\d+号/.exec(str)[0]));
			    				  }
			    			  }else{
			    				 string = str.substring(0,str.indexOf("街")+1);
			    			  }
			    		  }else{
			    			  string = str.substring(0,str.indexOf("路")+1);
			    		  }
			    	   }else{
			    		   string = str.substring(0,str.indexOf("段")+1);
			    	   }	
		    	   $("#zj_destination").val(string+poi.marker.getTitle());
		    	   $("#zj_endLng").val(poi.marker.point.lng);
			       $("#zj_endLat").val(poi.marker.point.lat);
			        end[0]=poi.marker.point.lng;
			        end[1]=poi.marker.point.lat;
			    	var title = poi.marker.getTitle();
			        var markers = zj_map2.getOverlays();
					for(var i=0;i<markers.length;i++){
						markers[i].show();
					}
			        if(start.length!=0){
			        	var startPoint=new BMap.Point(start[0], start[1]);
						var endPoint=new BMap.Point(end[0],end[1]);
						driving.search(startPoint, endPoint);
					    poi.marker.hide(); 
			        }
		    });
		}
	});
	/*
	//自动补全交付地点
	$("#zj_destination").AutoComplete({
	    data: ctx+"/order/getHistoryInputedPlace",
		ajaxDataType: "json",
	    itemHeight: 24,
	    width:"auto",
	    async: true,
	    listStyle: "custom",
	    emphasis: false,
	    matchHandler: function(keyword, data){
	        return true;
	    },
	    createItemHandler: function(index, data){
	    	if(data.address.length>14){
        		return "<span title='"+data.address+"'>"+data.address.substring(0,14)+"</span>";
        	}
            return "<span title='"+data.address+"'>"+data.address+"</span>";
	    },
	    afterSelectedHandler: function(data){
	    	var address = (data.address).split(" ");
	    	$("#zj_destination").val(address[0]);
	    	$("#zj_endLat").val(data.latitude);
	    	$("#zj_endLng").val(data.longitude);
	    	if($("#zj_destination").val()!=""){
				var startPoint=new BMap.Point(parseFloat($("#zj_startLng").val()),parseFloat($("#zj_startLat").val()));
				var endPoint=new BMap.Point(parseFloat($("#zj_endLng").val()),parseFloat($("#zj_endLat").val()));
				map2.centerAndZoom(startPoint, 10); 
				driving.search(startPoint, endPoint);
			}		
	        markerComplete(data.longitude,data.latitude);
	    }
	}).AutoComplete('show');
/*
	$("#zj_startAddress").AutoComplete({
	    data: ctx+"/order/getHistoryInputedPlace",
		ajaxDataType: "json",
	    itemHeight: 24,
	    max:1,
	    width: "auto",
	    async: true,
	    listStyle: "custom",
	    emphasis: false,
	    matchHandler: function(keyword, data){
	        return true;
	    },
	    createItemHandler: function(index, data){
	    	if(data.address.length>14){
        		return "<span title='"+data.address+"'>"+data.address.substring(0,14)+"</span>";
        	}
            return "<span title='"+data.address+"'>"+data.address+"</span>";
	    },
	    afterSelectedHandler: function(data){
	    	var address = (data.address).split(" ");
	    	$("#zj_startAddress").val(address[0]);
	    	$("#zj_startLat").val(data.latitude);
	    	$("#zj_startLng").val(data.longitude);
	        if($("#zj_destination").val()!=""){
	    		var startPoint=new BMap.Point(parseFloat($("#zj_startLng").val()),parseFloat($("#zj_startLat").val()));
	    		var endPoint=new BMap.Point(parseFloat($("#zj_endLng").val()),parseFloat($("#zj_endLat").val()));
	    		map2.centerAndZoom(startPoint, 10); 
	    		driving.search(startPoint, endPoint);	
	    	}
	        markerComplete(data.longitude,data.latitude);
	    }
	}).AutoComplete('show');
	*/
	$(".colse").click(function(){
		$('#myModal').modal("hide");
	});
	$("#tempAddress").AutoComplete({
	    data: ctx+"/order/getHistoryInputedPlace",
		ajaxDataType: "json",
	    itemHeight: 24,
	    max:1,
	    width: "auto",
	    async: true,
	    listStyle: "custom",
	    emphasis: false,
	    matchHandler: function(keyword, data){
	        return true;
	    },
	    createItemHandler: function(index, data){
	    	if(data.address.length>14){
        		return "<span title='"+data.address+"'>"+data.address.substring(0,14)+"</span>";
        	}
            return "<span title='"+data.address+"'>"+data.address+"</span>";
	    },
	    afterSelectedHandler: function(data){
	    	var length = zj_points.length;
	    	var address = zj_input.find(".zj_waypointAddress");
	    	address.attr("zj_waypointLng",data.longitude);
	    	address.attr("zj_waypointLat",data.latitude);
	    	address.val(data.address);
    		$("#tempAddress").val(data.address);
    		 var point=new BMap.Point(data.longitude,data.latitude);
    		 var tempIndex=parseInt(address.attr("zj_index"));
	    		 zj_index=0;
		    	 zj_flag=true;
				 zj_distance=[];
				 zj_timeStr=[];
			   if(zj_points[tempIndex]!=undefined){
				   zj_points.splice(tempIndex,1,point);
	    	   }else{
	    		   zj_points[length]=point;
	    	   }
			   if(zj_points.length!=1){
				  zj_map2.clearOverlays();           
					  for(var i=0;i<zj_points.length-1;i++){
						  zj_driving.search(zj_points[i],zj_points[i+1]);
					  }	 
			   }else{
				   getFreightEmployee();
				   var marker = new BMap.Marker(point); 
		    		zj_map2.addOverlay(marker);
			   }
	        markerComplete(data.longitude,data.latitude);
	    }
	}).AutoComplete('show');
	
});
